package com.yanqu.road.server.gameplayer.module.activity.summerquiz;

import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.activity.summerquiz.SummerQuizConfig;
import com.yanqu.road.entity.activity.summerquiz.config.SummerQuizInfo;
import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.pb.activity.SummerQuizProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.activity.summerquiz.SummerQuizActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

public class SummerQuizModule extends GeneralModule {

    public SummerQuizModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void afterLogin() {
        activityReload();
    }

    public void activityReload() {

        if (!SystemOpenMgr.systemOpen(player, eSystemId.SummerQuizActivity.getValue())) {
            return;
        }

        SummerQuizConfig summerQuizConfig = SummerQuizActivityMgr.getConfig();

        if(summerQuizConfig == null || !ActivityMgr.activityInShowTime(summerQuizConfig.getActivityInfo())){
            return;
        }

        syncConfig(summerQuizConfig);

        initUserData(summerQuizConfig);

        if(!resetOneDay()) {
            syncUserData(summerQuizConfig);
        }
    }

    public int getLanguageIndex(String language){
        List<String> list = StringUtils.stringToStringList("th_th|en_us|zh_cm|zh_twf|ru_ru", "\\|");
        int index = list.indexOf(language);
        return index >= 0 ? index : 0;
    }

    private void syncConfig(SummerQuizConfig summerQuizConfig) {
        SummerQuizProto.SummerQuizConfigSyncMsg.Builder sync = SummerQuizProto.SummerQuizConfigSyncMsg.newBuilder();
        String language = player.getLanguage();
        ServerInfo serverInfo = ServerListMgr.getServerInfo(GameServer.getInstance().getServerId());
        //如果是中文繁体，但不是台服，切换成海外繁体
        if("zh_tw".equals(language) && (serverInfo.getChannelId() != eChannelType.TaiWan.getValue() && serverInfo.getChannelId() != eChannelType.TaiWanExperience.getValue() &&
                serverInfo.getChannelId() != eChannelType.InnerChannel.getValue())){
            language = "zh_twf";
        }
        int languageIndex = getLanguageIndex(language);
        for (SummerQuizInfo quizInfo : summerQuizConfig.getQuizInfoList()) {
            SummerQuizProto.SummerQuizInfoTemp.Builder builder = SummerQuizProto.SummerQuizInfoTemp.newBuilder();
            builder.setId(quizInfo.getId());
            builder.setDate(quizInfo.getDate());
            builder.setBeginQuiz(quizInfo.getBeginQuiz());
            builder.setBeginQuizEx(quizInfo.getBeginQuizEx());
            builder.setEndQuiz(quizInfo.getEndQuiz());
            builder.setEndQuizEx(quizInfo.getEndQuizEx());
            builder.addAllInitialOdds(quizInfo.getInitialOdds());
            builder.setResult(quizInfo.getResult());
            builder.setBannerType(quizInfo.getBannerType());

            if(languageIndex >= quizInfo.getQuestion().size()){
                builder.setQuestion(quizInfo.getQuestion().get(0));
            }else {
                builder.setQuestion(quizInfo.getQuestion().get(languageIndex));
            }

            if(languageIndex >= quizInfo.getProject().size()){
                builder.setProject(quizInfo.getProject().get(0));
            }else {
                builder.setProject(quizInfo.getProject().get(languageIndex));
            }
            sync.addQuizInfo(builder);
        }
        player.sendPacket(Protocol.U_SUMMER_QUIZ_CONFIG_SYNC, sync);
    }

    private void initUserData(SummerQuizConfig summerQuizConfig) {


    }

    private boolean resetOneDay() {
        return false;
    }

    private void syncUserData(SummerQuizConfig summerQuizConfig) {

        SummerGamesConfig gamesConfig = SummerGamesActivityMgr.getSummerGamesConfig();
        // 没有活动
        if (gamesConfig == null || !ActivityMgr.activityInShowTime(gamesConfig.getActivityInfo())) {
            return;
        }

        SummerQuizProto.CrossSummerQuizUserLoginReqMsg.Builder crossReq = SummerQuizProto.CrossSummerQuizUserLoginReqMsg.newBuilder();
        crossReq.setActivityId(summerQuizConfig.getActivityInfo().getActivityId());
        crossReq.setGameActivityId(gamesConfig.getActivityId());
        player.sendPacket(Protocol.C_SUMMER_QUIZ_USER_GUESS_LOGIN, crossReq);
    }
}
